//
//  MaiTianLogViewController.m
//  lmq
//
//  Created by 马田成 on 2017/3/15.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "MaiTianLogViewController.h"
#import "MaiTianLogTableViewCell.h"
#import "MaiTianLogCollectionViewCell.h"
#import "MaiTianLogModel.h"
#import "MJRefresh.h"
#import "MJRefreshHeader.h"
#import "MJRefreshFooter.h"
#import "UIImageView+WebCache.h"
#import "detailsViewController.h"
@interface MaiTianLogViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;



@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UICollectionView *collectionView;



@end

@implementation MaiTianLogViewController
//麦田日志
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:YES];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self creationDateJZ];
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self creationDate];
        
        [self.tableView.mj_header beginRefreshing];
    }];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationDate];
    _page = 1;
    //    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    //
    //        [self refreshData];
    //
    //    }];
    //
    
    
    [self creationView];
    [self regeditCell];
    
    
    
    
    
}
- (void)creationView{
    
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64 - 49 - 50);
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.estimatedRowHeight = 400;
    
    
    
    
    
    
    
}
- (void)regeditCell{
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MaiTianLogTableViewCell" bundle:nil] forCellReuseIdentifier:@"MaiTianLogTableViewCell"];
    
}

- (void)creationDate{
    _dataArr = [[NSMutableArray alloc] init];
    _arrOfPic = [[NSMutableArray alloc] init];
    _pic = [[NSMutableArray alloc] init];
    
    _page = 1;
    
    NSString *strPage = [NSString stringWithFormat:@"%ld", (long)_page];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
    [productParams setValue:tokenStr forKey:@"token"];
    [productParams setValue:strPage  forKey:@"page"];
    [productParams setValue:@"10" forKey:@"rows"];
    
    [[MNetworkHandler sharedInstance]conURL:Home_MaiTianLog
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       NSMutableArray *dict1=[returnData objectForKey:@"data"];
                                       ;
                                       for (NSDictionary *dic in dict1 ) {
                                           MaiTianLogModel *model = [[MaiTianLogModel alloc]init];
                                           [model setValuesForKeysWithDictionary:dic];
                                           [_dataArr addObject:model];
                                           
                                           
                                       }
                                       
                                       
                                       
                                       [self.tableView reloadData];
                                       [self.tableView.mj_header endRefreshing];
                                   }
                                   else {
                                       
                                       
                                       
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
    
}


//加载
- (void)creationDateJZ{
    
    
    
    
    NSString *strPage = [NSString stringWithFormat:@"%ld", (long)_page];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
    [productParams setValue:tokenStr forKey:@"token"];
    [productParams setValue:strPage  forKey:@"page"];
    [productParams setValue:@"100" forKey:@"rows"];
    
    [[MNetworkHandler sharedInstance]conURL:Home_MaiTianLog
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       self.page++;
                                       NSMutableArray *dict1=[returnData objectForKey:@"data"];
                                       ;
                                       for (NSDictionary *dic in dict1 ) {
                                           MaiTianLogModel *model = [[MaiTianLogModel alloc]init];
                                           [model setValuesForKeysWithDictionary:dic];
                                           [_dataArr addObject:model];

                                       }
        
                                       [self.tableView reloadData];
                                       [self.tableView.mj_footer endRefreshing];
                                   }
                                   else {
                                       
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                       [self.tableView.mj_footer endRefreshing];
                                       
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
    
}


#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
    
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *a = [self.dataArr[indexPath.row] valueForKey:@"pic"];
    
    //    return 200 + 140 *(a.count / 3);
    if (a.count >6 && a.count<=9) {
        return 140 +420;
//        return UITableViewAutomaticDimension + 420;
        
    }else if (a.count<= 6 &&a.count>=3){
        
        return 140 +280;
//        return UITableViewAutomaticDimension + 280;
    }else {
        
        return UITableViewAutomaticDimension;
//        return UITableViewAutomaticDimension + 340;
    }
    
    
    //   return 500;
}
#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MaiTianLogTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MaiTianLogTableViewCell" forIndexPath:indexPath];
    NSMutableArray *arr = [self.dataArr[indexPath.row] valueForKey:@"pic"];
    
    MaiTianLogModel *model = self.dataArr[indexPath.row];
    cell.arrOfPic = [self.dataArr[indexPath.row] valueForKey:@"pic"];
    //    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.maiTianModel = model;
   
    
    cell.num = [NSString stringWithFormat:@"%lu", (unsigned long)arr.count];
    
    
    
    
    return cell;
    
}
#pragma mark - ---------- 每个Cell的点击事件 ----------
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    detailsViewController *detailsVC=[[detailsViewController alloc]init];
    detailsVC.hidesBottomBarWhenPushed=YES;
    
    MaiTianLogModel *model = self.dataArr[indexPath.row];
    detailsVC.myStr = model.shop_id;
    
    
    [self.navigationController pushViewController:detailsVC animated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
